package student;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.table.DefaultTableCellRenderer;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import flexibility.CachedRowSetTableModel;
import flexibility.userData;
import gui.studentScreen;

/**
 * 
 * This class is responsible to fill the assignment;s table according to the selected course
 *
 */
public class fillAssignments {
	
	public static String courseID;
	private CachedRowSetImpl crs;
	private userData DBData = new userData();
	private static ClientConsole studentConnection;
	private Integer myID;
	private CachedRowSetTableModel data;
	
	public fillAssignments(ClientConsole conn,Integer userID ){
		studentConnection=conn;
		myID=userID;
	}
		
	public void setCourseId(String courseId){
		courseID=courseId;
	}
	
	/**
	 * This method creates all necessary views in the database in order to fill the assignments table
	 */
	public void createViews(fillCourses FC){
		//mainassignment view
		DBData.deleteAll();
		DBData.addData(52);
		DBData.addData(0);
		DBData.addData(courseID);  //courseID
		DBData.addData(FC.getSelectedSemesterID()); // the selected semester ID
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		//mainstudent view
		DBData.deleteAll();
		DBData.addData(53);
		DBData.addData(0);
		DBData.addData(myID);
		DBData.addData(FC.getSelectedSemesterID()); // the selected semester ID
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		//mainfeedback view
		DBData.deleteAll();
		DBData.addData(54);
		DBData.addData(0);
		DBData.addData(myID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		//maincomment view
		DBData.deleteAll();
		DBData.addData(55);
		DBData.addData(0);
		DBData.addData(myID);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		
		//mainas view
		DBData.deleteAll();
		DBData.addData(56);
		DBData.addData(0);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();

		//mainasf
		DBData.deleteAll();
		DBData.addData(57);
		DBData.addData(0);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
	}
	
	/**
	 * This method returns a table with full information for Down Student's Screen table
	 * @return crs - as a final table
	 */
	public CachedRowSetImpl getFullTable(){
		
		DBData.deleteAll();
		DBData.addData(58);
		DBData.addData(1);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		return crs;
	}
	
	/**
	 * This method returns a table with only information to present in Down Table
	 */
	public CachedRowSetImpl getInformationTable(){
		DBData.deleteAll();
		DBData.addData(60);
		DBData.addData(1);
		studentConnection.acceptMsg(DBData);
		crs = studentConnection.getResults();
		return crs;
	}
	
	public void deleteAllViews(){
		int i;
		String[] viewArr=new String[] {"mainasf","mainas","maincomment","mainfeedback","mainstudent","mainassignment"};
		for (i=0;i<6;i++){
			DBData.deleteAll();
			DBData.addData(59);
			DBData.addData(0);
			DBData.addData(viewArr[i]); // name of view - delete the specific view according to the name
			studentConnection.acceptMsg(DBData);
			crs = studentConnection.getResults();
		}
	}
	
	/**
	 * This method stores the needed fileIds of every assignment in array list
	 * @param crs  - all information for Down StudentScreen Table
	 * @return infoArr - array of 'assgnmentInfo' objects, every object stores the needed fileIDs of the specific assignment
	 */
	public ArrayList<assignmentInfo> fillAssignmentList(CachedRowSetImpl crs){
		assignmentInfo info;
		ArrayList<assignmentInfo> infoArr=new ArrayList<assignmentInfo>();
		try {
			while (crs.next()){
				Integer aID=(int) crs.getDouble(1);
				Integer fAID=crs.getInt(2);
				Integer fSID=crs.getInt(5);
				Integer fFID=crs.getInt(7);
				Integer fCID=crs.getInt(9);
				info=new assignmentInfo(aID, fAID, fSID, fFID, fCID);
				infoArr.add(info);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return infoArr;
	}
	
	
	/**
	 * This method parses from double number as string to integer number 
	 * @param stringID - double number as string
	 * @return courseID - integer number
	 */
	/*
	public Integer getCourseIdAsInt(String stringID){
		int index=stringID.indexOf(".");
		stringID=stringID.substring(0,index);
		int courseID=Integer.parseInt(stringID);
		System.out.println("selected course: "+courseID);
		return courseID;
	}*/
	
	public void update(studentScreen ssc, CachedRowSetImpl crs ){
		try {
			
			
			ssc.table.setModel(data = new CachedRowSetTableModel(crs));
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			System.out.println("Number of columns: "+ssc.table.getColumnCount());
			for (int i = 0; i < ssc.table.getColumnCount(); i++){
				ssc.table.getColumnModel().getColumn(i)
						.setCellRenderer(centerRenderer);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
